Arrays and hash tables are two of the most common data structures available in modern scripting languages, and Windows PowerShell supports both of them. An array, which is sometimes referred to as a collection, stores a list of items. A hash table, which is sometimes called a dictionary or an associative array, stores a paired list of items.
To use an array in a program, you must declare a variable to reference the array, and you can specify the type of array the variable can reference. Here is the syntax for declaring an array variable −
PS> $array=@(1,2,3,5,6,7,8); PS>
By default type of objects of array is System.Object. GetType() method returns the type of the array. Type can be passed.
PS>[int32[]]$intA = 1500,2230,3350,4000 PS> PS>$A = 1, 2, 3, 4 PS>$A.getType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array
PS>$array.Length PS>
PS>$array[0] + $array[1] + $array[2] PS>
PS>$array = @() PS>
PS>$array += 1 PS>$array += 2 PS>$array += 3 PS>
Each item (or element) in an array can be referenced by number, starting with 0. For example, $array[0] refers to the first item in the array, $array[1] refers to the second item, and so forth. For example, to output each item in the array using the for statement and index number, you'd run the command:
for($i = 0; $i -lt $array.Length; $i++) { $array[$i] }
PowerShell's foreach statement provides a convenient way to access each item in an array. For example, to output each item in the $list array, you'd run the command:
foreach($element in $array) { $element }
The ForEach-Object cmdlet takes an array as input from the pipeline and passes each item in the array to a script block for processing. The current item in the array is represented by the special $_ variable. To output each item in the $list array using the ForEach-Object command, you'd run the command:
PS>$array | ForEach-Object { $_ }
Out of these three techniques, the for statement with the index number is used the least. The main reason is that the foreach statement and ForEach-Object cmdlet provide shorter ways to accomplish the same thing.
When learning about these three techniques, a common question is: What is the difference between using the foreach statement and the ForEach-Object cmdlet? The main difference is that when you use the foreach statement, you have to retrieve all the items in the array before you can access each item. In contrast, the ForEach-Object cmdlet takes advantage of PowerShell's pipeline to access one item at a time.
A trickier question to answer is: Which one should you use? The answer depends on the items in the array. If you have a relatively small list of items that don't take a long time to retrieve, the foreach statement will work just fine. If you have a large list of items or the items take a long time to retrieve (e.g., files on a remote file server), you'll probably want to use the ForEach-Object cmdlet. Another consideration concerns the Write-Progress cmdlet. If you want to use this cmdlet in a script to keep users informed about an operation's percentage of completion, you'll need to retrieve all the items first with the foreach statement so that you can calculate the percentage completed.
$array = @(1, 2, 3), @(4, 5, 6), @(7, 8, 9) $array[0] # 1 # 2 # 3 $array[0][0] # 1 $array[2][2] # 9